 package com.manor.main.utils;

import android.app.Activity;
import android.app.Dialog;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.listener.OnResultCallbackListener;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import com.manor.common.interfaces.CommonCallback;
import com.manor.common.upload.UploadBean;
import com.manor.common.upload.UploadCallback;
import com.manor.common.upload.UploadQnImpl;
import com.manor.common.upload.UploadStrategy;
import com.manor.common.utils.DialogUitl;
import com.manor.common.utils.GlideEngine;
import com.manor.common.utils.PictureSelectorUtils;
import com.manor.common.utils.ProcessImageUtil;
import com.manor.common.utils.StringUtil;
import com.manor.main.R;
import com.manor.main.http.MainHttpUtil;

/**

 * @version 1.0

 * @date 2020/5/20  16:32
 */
public class EditAvatarUtil {

    private UploadStrategy mUploadStrategy;
    private String mAvatarRemoteFileName;
    public static EditAvatarUtil instance = null;

    static {
        if (instance == null) {
            instance = new EditAvatarUtil();
        }
    }

    /**
     * 选择图片
     */
    public void editAvatar(@NonNull Activity mContext, Fragment fragment, @NonNull ProcessImageUtil mImageUtil) {
        editAvatar(mContext, fragment, mImageUtil, PictureConfig.CHOOSE_REQUEST);
    }

    public void editAvatar(@NonNull Activity mContext, Fragment fragment, @NonNull ProcessImageUtil mImageUtil,
                           int reqCode) {
        DialogUitl.showStringArrayDialog(mContext, new Integer[]{
                R.string.camera, R.string.alumb}, (text, tag) -> {
            if (tag == R.string.camera) {
                mImageUtil.getImageByCamera();
            } else {
                if (fragment != null)
                    selectImage(fragment, reqCode);
                else
                    selectImage(mContext, reqCode);
            }
        });
    }

    public void editAvatar(@NonNull Activity mContext, Fragment fragment, OnSelectImgListener listener) {
        DialogUitl.showStringArrayDialog(mContext, new Integer[]{
                R.string.camera, R.string.alumb}, (text, tag) -> {
            if (tag == R.string.camera) {
                if (fragment != null)
                    selectCamera(fragment, listener);
                else
                    selectCamera(mContext, listener);
            } else {
                if (fragment != null)
                    selectLocalImage(fragment, listener);
                else
                    selectLocalImage(mContext, listener);
            }
        });
    }


    /**
     * 选择拍照
     */
    public void selectCamera(Activity activity, OnSelectImgListener listener) {
        PictureSelector.create(activity)
                .openCamera(PictureMimeType.ofImage())// 单独拍照，也可录像或也可音频 看你传入的类型是图片or视频
                .selectionData(null)// 是否传入已选图片
                .isCamera(true)// 是否显示拍照按钮
                .isGif(false)
                .isOpenClickSound(false)// 是否开启点击声音
                .isCompress(false)// 是否压缩
                .isEnableCrop(true)
                .withAspectRatio(600,600)
                .cutOutQuality(100)
                .minimumCompressSize(20480)
                .cropImageWideHigh(600,600)
                .circleDimmedLayer(true)
                .showCropFrame(false)
                .showCropGrid(false)
                .forResult(new OnResultCallbackListener() {
                    @Override
                    public void onResult(List result) {
                        listener.onSelectedSuc(result);
                    }

                    @Override
                    public void onCancel() {

                    }
                });

    }
    public void selectCamera(Fragment activity, OnSelectImgListener listener) {
        PictureSelector.create(activity)
                .openCamera(PictureMimeType.ofImage())// 单独拍照，也可录像或也可音频 看你传入的类型是图片or视频
                .selectionData(null)// 是否传入已选图片
                .isCamera(true)// 是否显示拍照按钮
                .isGif(false)
                .isOpenClickSound(false)// 是否开启点击声音
                .isCompress(false)// 是否压缩
                .isEnableCrop(true)
                .withAspectRatio(600,600)
                .cutOutQuality(100)
                .minimumCompressSize(20480)
                .cropImageWideHigh(600,600)
                .circleDimmedLayer(true)
                .showCropFrame(false)
                .showCropGrid(false)
                .forResult(new OnResultCallbackListener() {
                    @Override
                    public void onResult(List result) {
                        listener.onSelectedSuc(result);
                    }

                    @Override
                    public void onCancel() {

                    }
                });

    }

    public void selectLocalImage(Activity mContext,OnSelectImgListener listener){
        PictureSelector.create(mContext)
                .openGallery(PictureMimeType.ofImage())
                .imageEngine(GlideEngine.createGlideEngine())
                .minSelectNum(1)
                .maxSelectNum(1)
                .selectionData(null)
                .isCamera(false)
                .isGif(false)
                .isOpenClickSound(false)
                .isCompress(false)// 是否压缩
                .isEnableCrop(true)
                .withAspectRatio(600,600)
                .cutOutQuality(100)
                .minimumCompressSize(20480)
                .cropImageWideHigh(600,600)
                .circleDimmedLayer(true)
                .showCropFrame(false)
                .showCropGrid(false)
                .forResult(new OnResultCallbackListener() {
                    @Override
                    public void onResult(List result) {
                        listener.onSelectedSuc(result);
                    }

                    @Override
                    public void onCancel() {

                    }
                });
    }

    public void selectLocalImage(Fragment mContext,OnSelectImgListener listener){
        PictureSelector.create(mContext)
                .openGallery(PictureSelectorUtils.MIME_TYPE_IMAGE)
                .imageEngine(GlideEngine.createGlideEngine())
                .minSelectNum(1)
                .maxSelectNum(1)
                .selectionData(null)
                .isCamera(false)
                .isGif(false)
                .isOpenClickSound(false)
                .isCompress(false)// 是否压缩
                .isEnableCrop(true)
                .withAspectRatio(600,600)
                .cutOutQuality(100)
                .minimumCompressSize(20480)
                .cropImageWideHigh(600,600)
                .circleDimmedLayer(true)
                .showCropFrame(false)
                .showCropGrid(false)
                .forResult(new OnResultCallbackListener() {
                    @Override
                    public void onResult(List result) {
                        listener.onSelectedSuc(result);
                    }

                    @Override
                    public void onCancel() {

                    }
                });
    }


    private void selectImage(Activity mContext) {
        selectImage(mContext, PictureConfig.CHOOSE_REQUEST);
    }

    private void selectImage(Activity mContext, int reqCode) {
        PictureSelector.create(mContext)
                .openGallery(PictureMimeType.ofImage())
                .imageEngine(GlideEngine.createGlideEngine())
                .maxSelectNum(1)
                .minSelectNum(1)
                .isCamera(false)
                .enableCrop(true)
//                .cropWH(600,600)
//                .sizeMultiplier(0.5f)// glide 加载图片大小 0~1之间
//                .compress(true)
                .isGif(false)
                .openClickSound(false)
                .forResult(reqCode);
    }

    private void selectImage(Fragment mContext) {
        selectImage(mContext, PictureConfig.CHOOSE_REQUEST);
    }

    private void selectImage(Fragment mContext, int reqCode) {
        PictureSelector.create(mContext)
                .openGallery(PictureMimeType.ofImage())
                .imageEngine(GlideEngine.createGlideEngine())
                .maxSelectNum(1)
                .minSelectNum(1)
                .isCamera(false)
                .enableCrop(true)
//                .cropWH(600,600)
//                .sizeMultiplier(0.5f)// glide 加载图片大小 0~1之间
//                .compress(true)
                .isGif(false)
                .openClickSound(false)
                .forResult(reqCode);
    }

    /**
     * 上传头像
     */
    public void uploadAvatarImage(Activity mContext, File upFile, CommonCallback<Boolean> commonCallback) {
        uploadAvatarImage(mContext, upFile,false, commonCallback);
    }

    public void uploadAvatarImage(Activity mContext, File upFile, boolean isBackdrop,
                                  CommonCallback<Boolean> commonCallback) {
        final Dialog mLoading = DialogUitl.loadingDialog(mContext);
        mLoading.show();
        if (mUploadStrategy == null) {
            mUploadStrategy = new UploadQnImpl(mContext);
        }
        List<UploadBean> list = new ArrayList<>();
        list.add(new UploadBean(upFile));
        mUploadStrategy.upload(list, true, new UploadCallback() {
            @Override
            public void onFinish(List<UploadBean> list, boolean success) {
                mLoading.dismiss();
                if (success) {
                    if (list != null && list.size() > 0) {
                        mAvatarRemoteFileName = list.get(0).getRemoteFileName();
                        if (StringUtil.isNotEmpty(mAvatarRemoteFileName)) {
                            if (isBackdrop)
                                setUserBackdrop(mAvatarRemoteFileName, commonCallback);
                            else
                                setUserInfo("avatar", mAvatarRemoteFileName, commonCallback);
                        }

                    }
                }
//                submit();
            }
        });
    }

    private void setUserInfo(String k, String v, final CommonCallback<Boolean> commonCallback) {
        MainHttpUtil.editUserInfo(k, v, commonCallback);
    }

    private void setUserBackdrop(String url, final CommonCallback<Boolean> commonCallback) {
        MainHttpUtil.editUserBackdrop(url, commonCallback);
    }

    public interface OnSelectImgListener {
        void onSelectedSuc(List<LocalMedia> mediaList);
    }


}
